<?php
// +--------------------------------------------------------------------------
// | ISDCE [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2017 载攸道 版权所有 <TEL:086-010-57209502>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * [ CMS ]
 */
namespace CMS\Controller;
use Common\Controller\AdminController;
Vendor('Qiniusdk.autoload'); //七牛入口文件引入 
use Qiniu\Auth; 
use Qiniu\Storage\UploadManager;
use Think\Upload;
/**
 * 文章管理
 */
class ArticleController extends AdminController{
	
	/**
	 * 文章管理
	 */
    public function Manage(){

		//页面配置
		$page = array(
			
			'title'=>'文章管理',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		
		);
		
		$this->assign('page',$page);
		//开始结束时间
		$begin_time = strtotime(I('param.begin_time'));
		$end_time = strtotime(I('param.end_time'));
		if($begin_time != null and $end_time != null){
			$map['time']  = array(array('egt',$begin_time),array('elt',$end_time),'and');
		}else{
			//默认今天明天
			$begin_time=time();
			$end_time=time()+86400;
		}
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		//判断是否搜索标题
		$name = I('param.name');
		if($name){
			
			$map['name'] = array('like',"%$name%");

		}
		$this->assign('name',$name);
		
		//判断是否搜索作者
		$author = I('param.author');
		if($author){
			
			$map['author'] = array('like',"%$author%");

		}
		$this->assign('author',$author);
		
		//用户UID
		$uid = I('param.uid')?I('param.uid'):'';

		if($uid!=0){
			
			$map['uid'] =array('like',"%$uid%");
		}
		
		//selected="selected"
		$this->assign('uid',$uid);
		
		$m_cms_content = M('cms_content');
		$m_common_user = M('common_user');
		
		//获得上级信息
		$form_id = I('param.form_id')?I('param.form_id'):0;
		
		
		//获得菜单信息
		if($form_id!=0){
			
			$map['form_id'] = $form_id;
		}
		
		//判断是否搜索状态
		$status = I('param.status');
		if($status){
			$map['status'] = $status;
		}else{
			$map['status'] = array('in','1,2');
		}
		$this->assign('status',$status);
		
		$map['type'] = get_cms_model_conf('CMS_ARTICLE');
		
		$_GET['p']=$_GET['p']?$_GET['p']:1;
		
		$list = $m_cms_content->where($map)->order('status asc,time desc')->page($_GET['p'].',50')->select();
	
		$count      = $m_cms_content->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		//查询关联信息
		foreach($list as $i=>$k){
			
			$fmap['id'] = $list[$i]['form_id'];
			$fdata = $m_cms_content->where($fmap)->find();
			$umap['id'] = $list[$i]['uid'];
			
			$udata = $m_common_user->where($umap)->find();
			
			
			$list[$i]['channel'] = $fdata['name'];
			$list[$i]['nickname'] = $udata['nickname'];
			$list[$i]['n_img'] = $udata['img'];
			
		
		}
		//赋值
		$this->assign('list',$list);
		$this->assign('data',$data);
		$this->assign('form_id',$fmap['id']);
		
		$classify = $this->getChannel();
		
		
		$this->assign('classify',$classify);
		$this->assign('classifys',$classify);
		
		
		$this->display();
	}
	
	
	/**
	 * 获得分类
	 */
	public function getChannel(){
		
			$m_cms_content = M('cms_content');
		
			//获得顶级分类
			$map['form_id'] = 0;
			$map['type'] = get_cms_model_conf('CMS_CHANNEL');
			$map['status'] = 1;
			
			$list = $m_cms_content->where($map)->order('sort desc')->select();
			
			//获得下级分类即商品数量
			foreach($list as $i=>$k){
				
				$down_map['form_id'] = $list[$i]['id'];
				$down_map['type'] = get_cms_model_conf('CMS_CHANNEL');
				$down_map['status']  =1;
				$down_list = $m_cms_content->where($down_map)->order('sort desc')->select();
				
				$list[$i]['down'] = $down_list;
				
			}
		
	
			return $list;
	}
	
	/**
	 * 新增文章
	 */
	public function Create(){
		

		$m_cms_content = M('cms_content');
			
		//判断是否是ajax请求
		if(IS_POST){

			
			//获得数据
			$data = I();
			
			$data['value'] = I('param.value','','');
			$data['uid'] = getUID();
			$data['form_id'] =  I('param.form_id')?I('param.form_id'):0;;
			$data['type'] = get_cms_model_conf('CMS_ARTICLE');
			$data['time'] = strtotime(I('param.time'));
			$data['ip'] = get_client_ip();
			$data['status'] = 1;
			
			$result = $m_cms_content->add($data);
			
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER'] 
			
				$this->success('新增成功', U('CMS/Article/Manage'));
			
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置  
			
				$this->error('新增失败');
			
			}
		
				
		}else{
			
			
				//获得顶级分类
				$map['form_id'] = 0;
				$map['type'] = get_cms_model_conf('CMS_CHANNEL');
				$map['status'] = 1;
				
				$list = $m_cms_content->where($map)->order('sort desc')->select();
				
				//获得下级分类即商品数量
				foreach($list as $i=>$k){
					
					$down_map['form_id'] = $list[$i]['id'];
					$down_map['type'] = get_cms_model_conf('CMS_CHANNEL');
					$down_map['status']  =1;
					$down_list = $m_cms_content->where($down_map)->order('sort desc')->select();
					
					$list[$i]['down'] = $down_list;
					
				}
		
	
				$this->assign('list',$list);
		
				$this->display();
		}
		
	
	}
	
	/**
	 * 新增文章
	 */
	public function Create_qiniu(){
		$accessKey = '8sc_uncdqBnQUYyWViRALobZH33YO_lZKD65Y04a';
		$secretKey = 'FczTgs6OmHBb7jke7pExGeujvxIHXO0trUCIbmBE';
		$auth = new Auth($accessKey, $secretKey);
		$bucketName = 'music';
		$token = $auth->uploadToken($bucketName); 
		#dump($token);die;
		$m_cms_content = M('cms_content');
			
		//判断是否是ajax请求
		if(IS_POST){

			
			//获得数据
			$data = I();
			
			$data['value'] = I('param.value','','');
			$data['uid'] = getUID();
			$data['form_id'] =  I('param.form_id')?I('param.form_id'):0;;
			$data['type'] = get_cms_model_conf('CMS_ARTICLE');
			$data['time'] = strtotime(I('param.time'));
			$data['ip'] = get_client_ip();
			$data['status'] = 1;
			
			$result = $m_cms_content->add($data);
			
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER'] 
			
				$this->success('新增成功', U('CMS/Article/Manage'));
			
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置  
			
				$this->error('新增失败');
			
			}
		
				
		}else{
			
			
				//获得顶级分类
				$map['form_id'] = 0;
				$map['type'] = get_cms_model_conf('CMS_CHANNEL');
				$map['status'] = 1;
				
				$list = $m_cms_content->where($map)->order('sort desc')->select();
				
				//获得下级分类即商品数量
				foreach($list as $i=>$k){
					
					$down_map['form_id'] = $list[$i]['id'];
					$down_map['type'] = get_cms_model_conf('CMS_CHANNEL');
					$down_map['status']  =1;
					$down_list = $m_cms_content->where($down_map)->order('sort desc')->select();
					
					$list[$i]['down'] = $down_list;
					
				}
		
	
				$this->assign('token',$token);
				$this->assign('list',$list);
		
				$this->display();
		}
		
	
	}
	
	/**
	 * 文章详情
	 */
	public function Detail(){
    
		$m_cms_content = M('cms_content');
		
		//页面配置
			$page = array(
				
				'title'=>'页面详情',
				'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'author'=>'YiPHP',
				'robots' => 'all',
			
			);
			
			$this->assign('page',$page);
			
		$map['id'] = I('param.id');
		$map['type'] = get_cms_model_conf('CMS_ARTICLE');
		$map['status'] = array('in','1,2');
			
		$data = $m_cms_content->where($map)->find();
			
		$this->assign('data',$data);
		
			
		$this->display();
		
		
	
	}
	/**
	 * 编辑文章
	 */
	public function Edit(){
    
		$m_cms_content = M('cms_content');
		
		$map['id'] = I('param.id');
		//判断是否是ajax请求
		if(IS_POST){
			$intro = I('param.intro');
			$str = str_replace("\r\n","<br/>",$intro);
             $str = htmlspecialchars_decode($str);
			 
           
			
			$m_cms_content = M('cms_content');
			
			set_cms_draft($map['id']); //保存草稿
			
			$data = I('');
			unset($data['id']);
			$data['value'] = I('param.value','','');
			$data['time'] = strtotime(I('param.time'));
			
			$data['ip'] = get_client_ip();
			 $data['intro'] = $str;
			$result = $m_cms_content->where($map)->save($data);
			
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER'] 
			
				$this->success('编辑成功', U('CMS/Article/Manage'));
			
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置  
			
				$this->error('编辑失败');
			
			}
		
		}else{
			
			$map['type'] = get_cms_model_conf('CMS_ARTICLE');
			$map['status'] = array('in','1,2');
		
			$data = $m_cms_content->where($map)->find();
			
			$str = str_replace("<br/>","\r\n",$data[intro]);
            $data[intro]= htmlspecialchars_decode($str);
			 
			
			
			$this->assign('data',$data);
		
			//获得顶级分类
			$smap['form_id'] = 0;
			$smap['type'] = get_cms_model_conf('CMS_CHANNEL');
			$smap['status'] = 1;
			
			$list = $m_cms_content->where($smap)->order('sort desc')->select();
			
			//获得下级分类即商品数量
			foreach($list as $i=>$k){
				
				$down_map['form_id'] = $list[$i]['id'];
				$down_map['type'] = get_cms_model_conf('CMS_CHANNEL');
				$down_map['status']  =1;
				$down_list = $m_cms_content->where($down_map)->order('sort desc')->select();
				
				$list[$i]['down'] = $down_list;
				
			}
			$this->assign('list',$list);
				
			$this->display();
		}
	}
	
	/**
	 * 批量改变状态
	 */
	public function Delete($id,$status=0){
		
	
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_cms_content = M('cms_content');
		
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			if($id){
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){
					
					
					$map['id'] = $id[$i];
					$data['status'] = $status;
					
					$m_cms_content->where($map)->save($data);
					
				}
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
		}
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	
	 public function Manage_notice(){
		$m_cms_message = M("cms_message");
		$m_common_user = M("common_user");
		//页面配置
		$page = array(
			'title'=>'消息管理',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		);
		
		$this->assign('page',$page);
		
		//判断是否搜索标题
		$name = I('param.name');
		if($name){
			$map['name'] = array('like',"%$name%");
		}
		$this->assign('name',$name);
		
		//开始结束时间
		$begin_time = strtotime(I('param.begin_time'));
		$end_time = strtotime(I('param.end_time'));
		if($begin_time != null and $end_time != null){
			$map['time']  = array(array('egt',$begin_time),array('elt',$end_time),'and');
		}else{
			//默认今天明天
			$begin_time=time();
			$end_time=time()+86400;
		}
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		//判断是否搜索状态
		$status = I('param.status');
		if($status){
			$map['status'] = $status;
		}else{
			$map['status'] = array('in','1,2');
		}
		$map['type'] = 1;
		$_GET['p']=$_GET['p']?$_GET['p']:1;
		$list = $m_cms_message->where($map)->order('time desc')->page($_GET['p'].',50')->select();
		$count      = $m_cms_message->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		//查询关联信息
		foreach($list as $i=>$k){
			$umap['id'] = $list[$i]['uid'];
			$udata = $m_common_user->where($umap)->find();
			$list[$i]['nickname'] = $udata['nickname'];
			$list[$i]['n_img'] = $udata['img'];
		}
		//赋值
		$this->assign('list',$list);
		$this->display();
	}
	
	
	 public function Manage_shezhi(){
		
		$m_cms_message = M("cms_message");
		$m_common_user = M("common_user");
		//页面配置  
		$page = array(
			'title'=>'消息管理',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		);
		
		$this->assign('page',$page);
		
		$map['type'] = 4;
		$map['status'] = 1;
		$list = $m_cms_message->where($map)->order('time desc')->select();
		//查询关联信息
		foreach($list as $i=>$k){
			$umap['id'] = $list[$i]['uid'];
			$udata = $m_common_user->where($umap)->find();
			$list[$i]['nickname'] = $udata['nickname'];
			$list[$i]['n_img'] = $udata['img'];
		}
		//赋值
		$this->assign('list',$list);
		$this->display();
	}
	
	 public function Manage_fankui(){
		$m_cms_message = M("cms_message");
		$m_common_user = M("common_user");
		//页面配置
		$page = array(
			'title'=>'消息管理',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		);
		
		$this->assign('page',$page);
		
		//判断是否搜索标题
		$value = I('param.value');
		if($value){
			$map['value'] = array('like',"%$value%");
		}
		$this->assign('value',$value);
		
		//开始结束时间
		$begin_time = strtotime(I('param.begin_time'));
		$end_time = strtotime(I('param.end_time'));
		if($begin_time != null and $end_time != null){
			$map['time']  = array(array('egt',$begin_time),array('elt',$end_time),'and');
		}else{
			//默认今天明天
			$begin_time=time();
			$end_time=time()+86400;
		}
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		$map['type'] = 3;
		$_GET['p']=$_GET['p']?$_GET['p']:1;
		$list = $m_cms_message->where($map)->order('time desc')->page($_GET['p'].',50')->select();
		$count      = $m_cms_message->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		//查询关联信息
		foreach($list as $i=>$k){
			$umap['id'] = $list[$i]['uid'];
			$udata = $m_common_user->where($umap)->find();
			$list[$i]['nickname'] = $udata['nickname'];
			$list[$i]['n_img'] = $udata['img'];
		}
		//赋值
		$this->assign('list',$list);
		$this->display();
	}
	
	
	/**
	 * 新增消息
	 */
	public function Create_notice(){
		$m_cms_message = M("cms_message");
			
		//判断是否是ajax请求
		if(IS_POST){
			$intro = I('param.intro');
			$str = str_replace("\r\n","<br/>",$intro);
             $str = htmlspecialchars_decode($str);
			//获得数据
			$data = I();
			$data['uid'] = getUID();
			$data['type'] = 1;
			$data['value'] = $str;
			$data['time'] = time();
			$data['ip'] = get_client_ip();
			$data['status'] = 1;
			
			$result = $m_cms_message->add($data);
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER'] 
			
				$this->success('新增成功', U('CMS/Article/Manage_notice'));
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置  
				$this->error('新增失败');
			}	
		}else{
			$this->display();
		}
	}
	
	
	/**
	 * 编辑消息
	 */
	public function Edit_notice(){
    
		$m_cms_message = M("cms_message");
		
		$map['id'] = I('param.id');
		//判断是否是ajax请求
		if(IS_POST){
			$intro = I('param.intro');
			$str = str_replace("\r\n","<br/>",$intro);
             $str = htmlspecialchars_decode($str);
			 
			$data['ip'] = get_client_ip();
			 $data['name'] = I('param.name');
			 $data['value'] = $str;
			$result = $m_cms_message->where($map)->save($data);
			
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER'] 
				$this->success('编辑成功', U('CMS/Article/Manage_notice'));
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置 
				$this->error('编辑失败');
			}
		}else{
			
			$map['status'] = array('in','1,2');
		
			$data = $m_cms_message->where($map)->find();
			$str = str_replace("<br/>","\r\n",$data['value']);
            $data['intro']= htmlspecialchars_decode($str);
			
			$this->assign('data',$data);
			$this->display();
		}
	}
	
	/**
	 * 编辑消息
	 */
	public function Edit_shezhi(){
    
		$m_cms_message = M("cms_message");
		
		$map['id'] = I('param.id');
		//判断是否是ajax请求
		if(IS_POST){
			
			$data['ip'] = get_client_ip();
			 $data['value'] = I('param.value','','');
			$result = $m_cms_message->where($map)->save($data);
			
			if($result){    //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER'] 
				$this->success('编辑成功', U('CMS/Article/Manage_shezhi'));
			} else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置 
				$this->error('编辑失败');
			}
		}else{
			
			$map['status'] = array('in','1,2');
		
			$data = $m_cms_message->where($map)->find();
			
			$this->assign('data',$data);
			$this->display();
		}
	}
	
	
	/**
	 * 消息详情
	 */
	public function Detail_notice(){
    
		$m_cms_message = M('cms_message');
		$m_common_user = M('common_user');
		//页面配置
			$page = array(
				'title'=>'消息详情',
				'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
				'author'=>'YiPHP',
				'robots' => 'all',
			);
			$this->assign('page',$page);
			
		$map['id'] = I('param.id');
		$map['status'] = array('in','1,2');
		$data = $m_cms_message->where($map)->find();
		$umap['id'] = $data['uid'];
		$udata = $m_common_user->where($umap)->find();
		$data['nickname'] = $udata['nickname'];
		$this->assign('data',$data);
		$this->display();
	}
	
	/**
	 * 批量改变状态
	 */
	public function Delete_notice($id,$status=0){
		//判断是否是ajax请求
		if(IS_AJAX){
			$m_cms_message = M('cms_message');
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			if($id){
				$id = explode(',',$id);
				foreach($id as $i=>$k){
					$map['id'] = $id[$i];
					$data['status'] = $status;
					$m_cms_message->where($map)->save($data);
				}
				//返回信息
				$info = get_errcode(0);
			}else{
				$info = get_errcode(10000001);
			}
		}
		//返回信息		
		$info = $info?$info:get_errcode(-2);
		$this->ajaxReturn($info);
	}
	
	
	
	
	
}